package com.zhejiangzhengyuan.municipal_common.response;

import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;


/**
 * @author lin_ju
 * @create on 2019/11/22
 */
@Data
@NoArgsConstructor
@ApiModel("查询结果Data(字段注解请查看Model)")
@Slf4j
public class ResponseVo<T> {
  // 常用result 预生成
  // 请求通过成功
  public static final ResponseVo SUCCESS = new ResponseVo(ConstantUtil.ResultCode.SUCCESS);
  // 拒绝请求（请求未通过）
  public static final ResponseVo FORBIDDEN = new ResponseVo(ConstantUtil.ResultCode.FORBIDDEN);
  // header 没有携带验证信息
  public static final ResponseVo UN_AUTHORIZED =
      new ResponseVo(ConstantUtil.ResultCode.UN_AUTHORIZED);
  // 服务器错误
  public static final ResponseVo SERVER_ERROR =
      new ResponseVo(ConstantUtil.ResultCode.SERVER_ERROR);
  // 未找到指定页面
  public static final ResponseVo NOT_FOUND_PATH = new ResponseVo(ConstantUtil.ResultCode.NOT_FOUND_PATH);
  //未找到指定资源
  public static final ResponseVo NOT_FOUND_RESOURCES = new ResponseVo(ConstantUtil.ResultCode.NOT_FOUND_RESOURCES);
  // 请求参数错误
  public static final ResponseVo CLIENT_PARAM_ERROR =
      new ResponseVo(ConstantUtil.ResultCode.CLIENT_PARAM_ERROR);

  /** 状态码 */
  @ApiModelProperty(value = "状态码")
  private int code;

  /** 响应信息 */
  @ApiModelProperty(value = "响应信息")
  private String message;

  /** 响应数据 */
  @ApiModelProperty(value = "响应数据")
  private T data;

  public ResponseVo(int code, String message) {
    this.code = code;
    this.message = message;
  }

  public ResponseVo(ConstantUtil.ResultCode resultCode) {
    this.code = resultCode.code();
    this.message = resultCode.message();
  }

  public ResponseVo(ConstantUtil.ResultCode resultCode, T data) {
    this.code = resultCode.code();
    this.message = resultCode.message();
    this.data = data;
  }

  public static ResponseVo successWithData (Object data){
    return new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS,data);
  }

  public static ResponseVo errorWithException (Exception ex){
      log.error("response Exception",ex);
    return new ResponseVo<>(ConstantUtil.ResultCode.SERVER_ERROR,ex.getMessage());
  }
}
